package com.coldchain.common.redis;

import org.redisson.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class RedisUtils {
   private static Logger logger= LoggerFactory.getLogger(RedisUtils.class);


   @Autowired
   private RedissonClient redisson;

   /**
    * 地理空间对象桶（Geospatial Bucket）
    * RGeo<String> geo = redisson.getGeo("test");
    geo.add(new GeoEntry(13.361389, 38.115556, "Palermo"),
    new GeoEntry(15.087269, 37.502669, "Catania"));
    geo.addAsync(37.618423, 55.751244, "Moscow");

    Double distance = geo.dist("Palermo", "Catania", GeoUnit.METERS);
    geo.hashAsync("Palermo", "Catania");
    Map<String, GeoPosition> positions = geo.pos("test2", "Palermo", "test3", "Catania", "test1");
    List<String> cities = geo.radius(15, 37, 200, GeoUnit.KILOMETERS);
    Map<String, GeoPosition> citiesWithPositions = geo.radiusWithPosition(15, 37, 200, GeoUnit.KILOMETERS);
    * @param objectname
    * @param <V>
    * @return
    */
   public <V> RGeo<V> getRGeo(String objectname){

      return redisson.getGeo(objectname);
   }

   public RKeys getRKeys(){
      return redisson.getKeys();
   }
   /**
    * 获取字符串对象
    * @param objectName
    * @return
    */
   public <T> RBucket<T> getRBucket( String objectName){
      RBucket<T> bucket=redisson.getBucket(objectName);
      return bucket;
   }

   public <K,V> RSetMultimap<K, V> getRSetMultimap( String objectName){

      RSetMultimap<K, V> map=redisson.getSetMultimap(objectName);
      return map;
   }

   public <K,V> RSetMultimapCache<K, V> getRSetMultimapCache( String objectName){

      RSetMultimapCache<K, V> map=redisson.getSetMultimapCache(objectName);
      return map;
   }
   public RBatch getRBatch(){
      return redisson.createBatch(BatchOptions.defaults());
   }

   /**
    * 获取Map对象
    * @param objectName
    * @return
    */
   public <K,V> RMap<K, V> getRMap( String objectName){

      RMap<K, V> map=redisson.getMap(objectName);
      return map;
   }



   public <K,V> RMapCache<K, V> getRMapCache( String objectName){

      RMapCache<K, V> map=redisson.getMapCache(objectName);
      
      return map;
   }

   public <K,V> RListMultimap<K, V> getRListMultimap( String objectName){

      RListMultimap<K, V> map=redisson.getListMultimap(objectName);
      
      return map;
   }

   public <K,V> RListMultimapCache<K, V> getRListMultimapCache( String objectName){

      RListMultimapCache<K, V> map=redisson.getListMultimapCache(objectName);
      return map;
   }

   /**
    * 获取有序集合
    RSortedSet<Integer> set = redisson.getSortedSet("anySet");
    set.trySetComparator(new MyComparator()); // 配置元素比较器
    set.add(3);
    set.add(1);
    set.add(2);

    set.removeAsync(0);
    set.addAsync(5);
    * @param objectName
    * @return
    */
   public <V> RSortedSet<V> getRSortedSet( String objectName){
      RSortedSet<V> sortedSet=redisson.getSortedSet(objectName);

      return sortedSet;
   }

   /**
    * 计分排序集（ScoredSortedSet）
    * RScoredSortedSet<SomeObject> set = redisson.getScoredSortedSet("simple");

    set.add(0.13, new SomeObject(a, b));
    set.addAsync(0.251, new SomeObject(c, d));
    set.add(0.302, new SomeObject(g, d));

    set.pollFirst();
    set.pollLast();

    int index = set.rank(new SomeObject(g, d)); // 获取元素在集合中的位置
    Double score = set.getScore(new SomeObject(g, d)); // 获取元素的评分
    * @param objectName
    * @param <V>
    * @return
    */
   public <V> RScoredSortedSet<V> getRScoredSortedSet(String objectName){
      RScoredSortedSet<V> sortedSet=redisson.getScoredSortedSet(objectName);
      return sortedSet;
   }
   /**
    * 获取集合
    * @param objectName
    * @return
    */
   public <V> RSet<V> getRSet(String objectName){
      RSet<V> rSet=redisson.getSet(objectName);
      return rSet;
   }
   
   /**
    * 获取列表
    * @param objectName
    * @return
    */
   public <V> RList<V> getRList(String objectName){
      RList<V> rList=redisson.getList(objectName);
      return rList;
   }
   
   /**
    * 获取队列
    * @param objectName
    * @return
    */
   public <V> RQueue<V> getRQueue(String objectName){
      RQueue<V> rQueue=redisson.getQueue(objectName);
      return rQueue;
   }


   
   /**
    * 获取双端队列
    * @param objectName
    * @return
    */
   public <V> RDeque<V> getRDeque(String objectName){
      RDeque<V> rDeque=redisson.getDeque(objectName);
      return rDeque;
   }
   
   /**
    * 此方法不可用在Redisson 1.2 中 
    * 在1.2.2版本中 可用
    * @param objectName
    * @return
    */
   /**
   public <V> RBlockingQueue<V> getRBlockingQueue(RedissonClient redisson,String objectName){
      RBlockingQueue rb=redisson.getBlockingQueue(objectName);
      return rb;
   }*/
   
   /**
    * 获取锁
    * @param objectName
    * @return
    */
   public RLock getRLock(String objectName){
      RLock rLock=redisson.getLock(objectName);
      return rLock;
   }
   
   /**
    * 获取原子数
    * @param objectName
    * @return
    */
   public RAtomicLong getRAtomicLong(String objectName){
      RAtomicLong rAtomicLong=redisson.getAtomicLong(objectName);
      return rAtomicLong;
   }
   
   /**
    * 获取记数锁
    * @param objectName
    * @return
    */
   public RCountDownLatch getRCountDownLatch(String objectName){
      RCountDownLatch rCountDownLatch=redisson.getCountDownLatch(objectName);
      return rCountDownLatch;
   }

   /**
    * 整长型累加器
    * @param key
    * @param num 需要增长的步数
    * @return
    */
   public  Long increment(String key,Integer num){

      RLongAdder atomicLong = redisson.getLongAdder(key);
      Long old=atomicLong.sum();
      if(old==null ||old>=0){
         SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
         String s=format.format(new Date());
         atomicLong.add(Long.valueOf(s));
      }
      atomicLong.add(num);
      atomicLong.increment();
      Long id=atomicLong.sum();
      atomicLong.deleteAsync();
      return id;
   }
   /**
    * 获取消息的Topic
    * @param objectName
    * @return
    */
   public <M> RTopic<M> getRTopic(String objectName){
       RTopic<M> rTopic=redisson.getTopic(objectName);
       return rTopic;
   }



   
   
}